// pages/reply/reply.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: 0,
    top: 0,
    intro:5,
    post:5,
    server:5,
    orderid:0,
    list:[],
    imgs:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({ orderid: options.orderid});
    that.Find_POrderDetail();
    wx.getSystemInfo({
      success: function (res) {
        if (res.platform == "ios") {
          that.setData({ left:-10, top: -20 });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  setIntro:function(e){
    var intro = e.currentTarget.dataset.intro;
    this.setData({ intro: intro});
  },
  setPost: function (e) {
    var post = e.currentTarget.dataset.post;
    this.setData({ post: post });
  },
  setServer: function (e) {
    var server = e.currentTarget.dataset.server;
    this.setData({ server: server });
  },
  formSubmit: function (e) {
    var that=this;
    var error=false;
    var imgcount=0;
    var uploadcount = 0;
    var replydetail = new Array();
    for (var i = 0; i < this.data.list.length; i++) {
      replydetail.push(this.data.list[i].ID + "&" + this.data.list[i].ProductID + "&" + this.data.list[i].Format + "&" + e.detail.value["context" + this.data.list[i].ID]);
      imgcount += that.data.imgs[i].length;
      if (e.detail.value["context" + this.data.list[i].ID].trim() == "") {
        error=true;
      }
    }
    if(error){
      app.showError("请输入评论内容");
      return;
    }
    wx.showLoading({ title: '正在提交...' });
    var data = {};
    data.act = "Save_Reply";
    data.orderid = this.data.orderid;
    data.introscore = this.data.intro;
    data.serverscore = this.data.server;
    data.postscore = this.data.post;
    data.replydetail = replydetail.join("|");
    
    app.Ajax(data, function (json) {
      if (json.state) {
        for(var n=0;n<json.data.length;n++){
          var imgs = that.data.imgs[n];
          for (var m=0;m<imgs.length;m++){
            var img = imgs[m];
            if (img.ID == json.data[n].OrderDetailID)
            {
            wx.uploadFile({
              url: 'https://shop.qiyedaishu.com/tools/Upload.ashx', //仅为示例，非真实的接口地址
              filePath: img.Img,
              name: 'img',
              formData: {
                'handle':'upload_file_a',
                'sid': json.data[n].ID,
                'tablename':'reply',
                'userid':1,
              },
              success: function (res) {
                var data = res.data
                uploadcount++;
                if (uploadcount==imgcount){
                  wx.hideLoading();
                  app.showSuccess("提交成功");
                  var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2];
                  prevPage.setData({ refresh: true });
                  setTimeout(function () {
                    wx.navigateBack();
                  }, 1000);
                }
              }
            });
          }
          }
        }
      } else {
        app.showError(json.msg);
      }
    });
  },
  Find_POrderDetail: function () {
    var that = this;
    var data = {};
    data.act = "Find_POrderDetail";
    data.orderid = this.data.orderid;
    wx.showLoading({
      title: '数据加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      var imgs=new Array();
      for(var i in json.data){
        imgs.push([]);
      }
      that.setData({ list: json.data,imgs:imgs });
      wx.stopPullDownRefresh();
    });
  },
  chooseImage:function(e){
    var that=this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var imgs=this.data.imgs[index];
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        for(var i=0;i<res.tempFilePaths.length;i++){
          imgs.push({ID:id,Img:res.tempFilePaths[i]});
        }
        that.data.imgs[index]=imgs;
        that.setData({imgs:that.data.imgs});
      }
    })
  }
})